package com.polaris.live.ui.me

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.StringUtils
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityInviteFriendBinding
import com.polaris.live.manager.AppConfigManager
import com.polaris.live.resp.back_resp.UserInviterCodeResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.InviteFriendViewModel
import com.uuzuche.lib_zxing.activity.CodeUtils

/**
 * 邀请好友页
 *
 * @author Created by 半仙 on 2024/4/10/010 9:52
 */
class InviteFriendActivity : BaseVbActivity<InviteFriendViewModel, ActivityInviteFriendBinding>() {

    private var mUserInviterCodeResp: UserInviterCodeResp? = null

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .transparentNavigationBar()
            .titleBarMarginTop(mBinding.ivBack)
            .statusBarDarkFont(true)
            .init()
    }

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.getInviteCode()
        mBinding.apply {
            if (CommonUtils.isMan(UserManager.getUser().sex)) {
                mBinding.tvTitleAddText.visibility = View.GONE
                mBinding.llTitleRight.visibility = View.GONE
                mBinding.tvInviteCallText.visibility = View.GONE
                mBinding.tvInviteFriendCenter.text = StringUtils.getString(string.invite_friend_detail_string_man)
                mBinding.tvInviteYouGet.text = StringUtils.getString(string.invite_friend_you_get_beans_man)
            }
        }
        mBinding.tvInviteAppName.text =
            StringUtils.getString(string.come_quickly_wejoy, StringUtils.getString(string.app_name))
        initListener()
        initObserver()
    }

    private fun initObserver() {
        mViewModel.mInviteLiveData.observe(this) { resultState ->
            parseState(resultState, { resp ->
                mUserInviterCodeResp = resp
                mBinding.apply {
                    tvCopyCode.text = StringUtils.getString(string.copy_code, resp.inviterCode)
                    tvCenterDetailText.text = StringUtils.getString(string.ios_copy_my_code, resp.inviterCode)
                    //链接
                    val config = AppConfigManager.getConfig(CommonConst.Configs.inviteUrl)
                    config?.let {
                        val link = "$it${resp.inviterCode}"
                        tvCopyLink.text = StringUtils.getString(string.copy_link, link)
                    }
                    //二维码
                    val configQr = AppConfigManager.getConfig(CommonConst.Configs.inviteUrlQR)
                    configQr?.let {
                        val link = "$it${resp.inviterCode}"
                        val mBitmap = CodeUtils.createImage(
                            link, 129.dp, 129.dp,
                            BitmapFactory.decodeResource(resources, 0)
                        )
                        mBinding.ivUserHead.setImageBitmap(mBitmap)
                    }
                }
            })
        }
    }

    private fun initListener() {
        //复制code码
        mBinding.tvCopyCode.setOnSingleClickListener {
            ClipboardUtils.copyText(mUserInviterCodeResp?.inviterCode)
            ToastGravity.showGravity(StringUtils.getString(string.copy_success))
        }
        //复制链接
        mBinding.tvCopyLink.setOnSingleClickListener {
            val config = AppConfigManager.getConfig(CommonConst.Configs.inviteUrl)
            config?.let {
                if (mUserInviterCodeResp?.inviterCode?.isNotEmpty() == true) {
                    val link = "$it${mUserInviterCodeResp?.inviterCode}"
                    ClipboardUtils.copyText(link)
                    ToastGravity.showGravity(StringUtils.getString(string.copy_success))
                }
            }
        }
        //下载图片
        mBinding.tvDownloadPhoto.setOnSingleClickListener {
            val bitmap = Bitmap.createBitmap(
                mBinding.consCenter.width,
                mBinding.consCenter.height,
                Bitmap.Config.ARGB_8888
            )
            val canvas = Canvas(bitmap)
            mBinding.consCenter.draw(canvas)
            bitmap?.let {
                val save2Album = ImageUtils.save2Album(it, Bitmap.CompressFormat.PNG)
                if (save2Album != null) {
                    ToastGravity.showGravity(StringUtils.getString(string.save_success))
                }
            }
        }
        //转跳我的奖励
        mBinding.tvMyReward.setOnSingleClickListener {
            Router.builder(RoutePath.MY_INVITE_REWARD_ACTIVITY).navigation(this)
        }
        mBinding.ivBack.setOnSingleClickListener {
            finish()
        }
    }
}